package com.talkyun.openx.validator;

import com.talkyun.openx.validator.annotation.*;
import com.talkyun.openx.validator.impl.*;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ValidatorManager {
    private static final ValidatorManager instance = new ValidatorManager();
    private Map<String, Object> mapping = new ConcurrentHashMap<String, Object>();

    public static ValidatorManager get() {
        return instance;
    }

    private ValidatorManager() {
        // java base validator
        this.add(ObjectValidator.class.getSimpleName(), new ObjectValidatorImpl());
        this.add(StringValidator.class.getSimpleName(), new StringValidatorImpl());
        this.add(NumberValidator.class.getSimpleName(), new NumberValidatorImpl());
        // simple business validator
        this.add(EmailValidator.class.getSimpleName(), new EmailValidatorImpl());
        this.add(PhoneValidator.class.getSimpleName(), new PhoneValidatorImpl());
    }


    public ValidatorManager add(String annotationName, AbstractValidator validatorImpl) {
        if (mapping.containsKey(annotationName)) {
            throw new RuntimeException("Annotation exist!");
        }
        mapping.put(annotationName, validatorImpl);
        return this;
    }
}